package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.model.YmtExpressSellerToMode;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class SellerWapperV2 {
	SqlManager manage;

	public SellerWapperV2() {
		if (manage == null || manage.getEnv() != "ymt" + EnvSetup.getEnv() || manage.isClosed()) {
			manage = new SqlManager("ymt" + EnvSetup.getEnv());
		}
	}

	/**
	 * @param sellerid
	 * @param catalogStatus
	 * @return
	 * @throws InterruptedException
	 */
	public int insertYmtExpressSellerToMode(Integer sellerid, Integer catalogStatus) throws InterruptedException {
		int i = 0;
		Map m = new HashMap<>();
		m.put("sellerid", sellerid);
		m.put("catalogStatus", catalogStatus);
		
		List<YmtExpressSellerToMode> list = manage.getSelectResults("com.ymttest.database.mapping.SellerMapperV2.selectYmtExpressSellerToMode", m);
		manage.commit();
		YmtExpressSellerToMode ymtExpressSellerToMode = new YmtExpressSellerToMode();
		ymtExpressSellerToMode.setCatalogStatus(catalogStatus);
		ymtExpressSellerToMode.setSellerId(sellerid);
		ymtExpressSellerToMode.setCreateTime(new Date());
		ymtExpressSellerToMode.setHasAvailable(true);
		ymtExpressSellerToMode.setId(TestDataManager.getRandomUUID().toString());
		ymtExpressSellerToMode.setIsDelete(false);
		ymtExpressSellerToMode.setUpdatetime(new Date());
		if (list.size() <= 0) {
			i = manage.insert("com.ymttest.database.mapping.SellerMapperV2.insertYmtExpressSellerToMode", ymtExpressSellerToMode);
		} 
		else {
			manage.update("com.ymttest.database.mapping.SellerMapperV2.updateYmtExpressSellerToModeToDelete", m);
			manage.commit();
			i = manage.insert("com.ymttest.database.mapping.SellerMapperV2.insertYmtExpressSellerToMode", ymtExpressSellerToMode);
		}
		manage.commit();
		return i;
	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		try {
			new SellerWapperV2().insertYmtExpressSellerToMode(20336378, 7);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
